home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Interactive 7
/
PC World Interactive 7.iso
/
program
/
ctutord.EXE
/
QUICK.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-10-09
|
2KB
|
136 lines
/* quick.c */
/* QUICKSORT In 'C' */
int d[]={40,15,30,25,60,10,75,45,65,35,50,20,70,55};
main()
{
int i;
printf("\nUnsorted: \n");
for(i=0; i<14; i++)
printf("%d ",d[i]);
quick(0,13);
printf("\nSorted: \n");
for(i=0; i<14; i++)
printf("%d ",d[i]);
printf("\n");
}
quick(left, right)
int left, right;
{
int i, j, k, x;
printf("\n\n%d %d",left,right);
if( left<right ){
if(d[left] > d[right]){
printf("\nSwap left with right \n");
swap(&d[left],&d[right]);
for(x=0; x<14; x++)
printf("%d ",d[x]);
}
j=left; k=right;
do{
do{ j++;}while(d[j]<d[left]);
do{ k--;}while(d[k]>d[left]);
if( j<k ){
swap(&d[j],&d[k]);
printf("\nSWap left with right \n");
for(x=0; x<14; x++)
printf("%d ",d[x]);
}
}while( j<=k);
swap(&d[left],&d[k]);
printf("\nswap left with right \n");
for(x=0; x<14; x++)
printf("%d ",d[x]);
quick(left,k-1);
quick(k+1,right);
}
}
swap(a,b)
int *a, *b;
{
int j;
j=*a;
*a=*b;
*b=j;
}
/*
OUTPUT:
Unsorted:
40 15 30 25 60 10 75 45 65 35 50 20 70 55
0 13
SWap left with right
40 15 30 25 20 10 75 45 65 35 50 60 70 55
SWap left with right
40 15 30 25 20 10 35 45 65 75 50 60 70 55
swap left with right
35 15 30 25 20 10 40 45 65 75 50 60 70 55
0 5
Swap left with right
10 15 30 25 20 35 40 45 65 75 50 60 70 55
swap left with right
10 15 30 25 20 35 40 45 65 75 50 60 70 55
0 -1
1 5
swap left with right
10 15 30 25 20 35 40 45 65 75 50 60 70 55
1 0
2 5
swap left with right
10 15 20 25 30 35 40 45 65 75 50 60 70 55
2 3
swap left with right
10 15 20 25 30 35 40 45 65 75 50 60 70 55
2 1
3 3
5 5
7 13
swap left with right
10 15 20 25 30 35 40 45 65 75 50 60 70 55
7 6
8 13
Swap left with right
10 15 20 25 30 35 40 45 55 75 50 60 70 65
SWap left with right
10 15 20 25 30 35 40 45 55 50 75 60 70 65
swap left with right
10 15 20 25 30 35 40 45 50 55 75 60 70 65
8 8
10 13
Swap left with right
10 15 20 25 30 35 40 45 50 55 65 60 70 75
swap left with right
10 15 20 25 30 35 40 45 50 55 60 65 70 75
10 10
12 13
swap left with right
10 15 20 25 30 35 40 45 50 55 60 65 70 75
12 11
13 13
Sorted:
10 15 20 25 30 35 40 45 50 55 60 65 70 75
*/